package com.cloud.system.api.web;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloud.system.api.bean.*;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cloud.system.api.entity.HumanInfo;
import com.cloud.system.api.service.HumanInfoService;
import com.cloud.system.api.service.SystemCacheService;
import com.cloud.system.common.consts.SystemConst;
import com.cloud.system.common.result.Quokka;
import com.cloud.system.common.result.SelectItem;
import com.cloud.system.common.utils.ExcelField;
import com.cloud.system.common.utils.ExcelHelper;

@RestController
@RequestMapping("system/api/humaninfo")
public class HumanInfoController {

	@Autowired
	private HumanInfoService humanInfoService;

	@Autowired
	private SystemCacheService systemCacheService;

	@RequestMapping("listtree")
	public Quokka listTree(long unitId, boolean isRoot, String isSupervise,boolean isScreen) {
		return new Quokka(true, humanInfoService.listHumanTree(unitId, isRoot,isSupervise,isScreen));
	}

	@RequestMapping("search")
	public Quokka search(String name) {
		return new Quokka(true, humanInfoService.search(name));
	}

	@RequestMapping("saveupdate")
	public Quokka saveUpdate(@RequestBody(required = true) HumanInfo humanInfo) {
		return new Quokka(true, humanInfoService.saveUpdate(humanInfo));
	}

	@RequestMapping("delete")
	public Quokka delete(long id) {
		return new Quokka(true, humanInfoService.delete(id));
	}

	@RequestMapping("confirm")
	public Quokka confirm(long id) {
		return new Quokka(true, humanInfoService.confirm(id));
	}


	@RequestMapping("loadhumanbyunitid")
	public Quokka loadHumanByUnitId(long id,String isSupervise) {
		return new Quokka(true, humanInfoService.loadHumansByUnitId(id, isSupervise));
	}

	@RequestMapping("load")
	public Quokka load(long id) {
		return new Quokka(true, humanInfoService.load(id));
	}

	@RequestMapping("showadd")
	public Quokka showAdd(long unitId) {
		return new Quokka(true, humanInfoService.showAdd(unitId));
	}

	@RequestMapping("loadrolesbyhuman")
	public Quokka loadRolesByHuman(long id) {
		return new Quokka(true, humanInfoService.loadRolesByHuman(id));
	}

	@RequestMapping("saveupdatehumanroles")
	public Quokka saveUpdateHumanRoles(@RequestBody(required = true) HumanRoleForm form) {
		return new Quokka(true, humanInfoService.saveUpdateHumanRoles(form));
	}

	@GetMapping("dict")
	public Quokka loadDicts(int sysId) {
		Quokka qk = new Quokka(true);
		Map<String, List<SelectItem>> map = new HashMap<String, List<SelectItem>>();
		map.put("positionLevel", systemCacheService.loadSelectItem(SystemConst.HUMAN_POSITION_LEVEL));// 职级
		map.put("educationMajor", systemCacheService.loadSelectItem(SystemConst.HUMAN_EDUCATION_MAJOR));// 职级
		map.put("politicalStatus", systemCacheService.loadSelectItem(SystemConst.HUMAN_POLITICAL_STATUS));// 政治面貌
		map.put("education", systemCacheService.loadSelectItem(SystemConst.HUMAN_EDUCATION));// 学历
		map.put("nation", systemCacheService.loadSelectItem(SystemConst.HUMAN_NATION));// 民族
		map.put("humanNature", systemCacheService.loadSelectItem(SystemConst.HUMAN_NATURE));// 执法人员性质
		map.put("lawCardType", systemCacheService.loadSelectItem(SystemConst.HUMAN_LAWCARD_TYPE));// 执法证类型
		map.put("domains", systemCacheService.loadSelectItem(SystemConst.UNIT_DOMAIN));// 执法证类型
		qk.setData(map);
		return qk;
	}

	@PostMapping("reorderhumans")
	public Quokka reorderHumans(@RequestBody List<HumanInfo> humans) {
		return new Quokka(true, humanInfoService.reorderHumans(humans));
	}

	/**
	 * 下载人員导入模板
	 * 
	 * @return
	 * @throws Exception
	 */
	@GetMapping("template")
	public ResponseEntity<byte[]> downloadTemplate() throws Exception {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/human.template.xlsx");
		return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.OK);
	}

	/**
	 * 解析上传的导入模板
	 * 
	 * @param file
	 * @return
	 */
	@PostMapping("analyse")
	public Quokka analyseImportFile(MultipartFile file) throws Exception {
		Quokka quokka = new Quokka(true);
		List<HumanImportBean> humans = ExcelHelper.xlsx2List(file.getInputStream(),
				new String[] { "humanName", "telephone", "positionLevelName", "identityNum", "politicalStatusName",
						"educationName", "nationName", "humanNatureName", "humanTypeName", "occupFlagName",
						"lawcardFlagName", "lawyerFlagName", "lawCardNum", "lawCardTypeName", "issueDate",
						"issueSubjectName", "effectiveStart", "effectiveEnd" },
				HumanImportBean.class);
		Map<String, List<HumanImportBean>> analyseResult = humanInfoService.analyseImportFile(humans);
		quokka.setData(analyseResult);
		return quokka;
	}

	/**
	 * 数据导入
	 * 
	 * @param file
	 * @return
	 */
	@PostMapping("import")
	public Quokka importHumans(Long unitId, @RequestBody List<HumanInfo> humans) {
		return new Quokka(true, humanInfoService.importHumans(unitId, humans));
	}

	@PostMapping("downloaderror")
	public ResponseEntity<byte[]> downloadError(@RequestBody List<UnitImportBean> units) throws Exception {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		List<ExcelField> fields = new ArrayList<ExcelField>();
		fields.add(new ExcelField("姓名", "humanName"));
		fields.add(new ExcelField("联系电话", "telephone"));
		fields.add(new ExcelField("职级", "positionLevelName"));
		fields.add(new ExcelField("身份证号码", "identityNum"));
		fields.add(new ExcelField("政治面貌", "politicalStatusName"));
		fields.add(new ExcelField("最高学历", "educationName"));
		fields.add(new ExcelField("民族", "nationName"));
		fields.add(new ExcelField("人员性质", "humanNatureName"));
		fields.add(new ExcelField("人员类别", "humanTypeName"));
		fields.add(new ExcelField("是否具有法律置业资格", "occupFlagName"));
		fields.add(new ExcelField("是否获得执法证", "lawcardFlagName"));
		fields.add(new ExcelField("是否公职律师", "lawyerFlagName"));
		fields.add(new ExcelField("执法证号", "lawCardNum"));
		fields.add(new ExcelField("执法证类型", "lawCardTypeName"));
		fields.add(new ExcelField("颁发日期", "issueDate"));
		fields.add(new ExcelField("颁发机构", "issueSubjectName"));
		fields.add(new ExcelField("有效期（起）", "effectiveStart"));
		fields.add(new ExcelField("有效期（止）", "effectiveEnd"));
		fields.add(new ExcelField("错误信息", "errorMsg"));
		ByteArrayOutputStream bos = (ByteArrayOutputStream) ExcelHelper.list2Xlsx(units, "人员导入错误数据", "", fields,
				UnitImportBean.class);
		return new ResponseEntity<byte[]>(bos.toByteArray(), headers, HttpStatus.OK);
	}
	
	@GetMapping("profile")
	public Quokka loadProfile(){
		return new Quokka(true , humanInfoService.loadProfileInfo());
	}
	@PostMapping("updateprofile")
	public Quokka updateProfile(@RequestBody HumanProfile profile){
		humanInfoService.updateProfile(profile);
		return new Quokka(true);
	}

	/**
	 * 分页查询部门信息
	 * @param criteria
	 * @return
	 */
	@RequestMapping("listPatrolPage")
	public Quokka listUnitPage(@RequestBody PatrolCriteriaBean criteria) {
		return new Quokka(true, humanInfoService.listPatrolPage(criteria));
	}

	/**
	 * 执法人员学历统计
	 * @return
	 */
	@RequestMapping("getPatrolEduStat")
	public Quokka getPatrolEduStat(Integer isSupervise) {
		return new Quokka(true, humanInfoService.getPatrolEduStat(isSupervise));
	}

	/**
	 * 执法人员性质统计
	 * @return
	 */
	@RequestMapping("getPatrolNatureStat")
	public Quokka getPatrolNatureStat() {
		return new Quokka(true, humanInfoService.getPatrolNatureStat());
	}
	/**
	 * 执法类型统计
	 * @return
	 */
	@RequestMapping("getPatrolLawTypeStat")
	public Quokka getPatrolLawTypeStat(Integer isSupervise) {
		return new Quokka(true, humanInfoService.getPatrolLawTypeStat(isSupervise));
	}

	/**
	 * 执法队员性别统计
	 * @return
	 */
	@RequestMapping("getPatrolGenderStat")
	public Quokka getPatrolGenderStat(Integer isSupervise) {
		return new Quokka(true, humanInfoService.getPatrolGenderStat(isSupervise));
	}

	/**
	 * 执法队员区域统计
	 * @return
	 */
	@RequestMapping("getPatrolAreaStat")
	public Quokka getPatrolAreaStat(Integer isSupervise) {
		return new Quokka(true, humanInfoService.getPatrolAreaStat(isSupervise));
	}
	/**
	 * 执法证统计
	 * @return
	 */
	@RequestMapping("getPatrolCardStat")
	public Quokka getPatrolCardStat() {
		return new Quokka(true, humanInfoService.getPatrolCardStat());
	}


	/**
	 * 执法人员年龄统计
	 * @return
	 */
	@RequestMapping("getPatrolAgeStat")
	public Quokka getPatrolAgeStat() {
		return new Quokka(true, humanInfoService.getPatrolAgeStat());
	}


	/**
	 * 是否拥有执法证统计
	 * @return
	 */
	@RequestMapping("getLawyerStat")
	public Quokka getLawyerStat() {
		return new Quokka(true, humanInfoService.getLawyerStat());
	}

	/**
	 * 是否拥有执法证统计
	 * @return
	 */
	@RequestMapping("getLawCardFlagStat")
	public Quokka getLawCardFlagStat() { return new Quokka(true, humanInfoService.getLawCardFlagStat()); }

	/**
	 * 是否公职律师统计
	 * @return
	 */
	@RequestMapping("getObtainLawCardStat")
	public Quokka getObtainLawCardStat() {
		return new Quokka(true, humanInfoService.getObtainLawCardStat());
	}


	/**
	 * 是否公职律师统计
	 * @return
	 */
	@RequestMapping("getPatrolCount")
	public Quokka getPatrolCount(Integer isSupervise) {
		return new Quokka(true, humanInfoService.getPatrolCount(isSupervise));
	}
	
	/**
	 * 人员所属机构调整
	 * @param adjustBean
	 * @return
	 */
	@RequestMapping("adjust")
	public Quokka humanUnitAdjust(@RequestBody HumanUnitAdjustBean adjustBean){
		humanInfoService.humanUnitAdjust(adjustBean);
		return new Quokka(true);
	}

	/**
	 * 密码修改
	 * @param profile
	 * @return
	 */
	@PostMapping("changePassword")
	public Quokka changePassword(@RequestBody HumanProfile profile){

		return new Quokka(true,humanInfoService.changePassword(profile));
	}

	/**
	 * 加载工作人员
	 * @param unitId
	 * @return
	 */
	@RequestMapping("loadstaffsbyunitid")
	public Quokka loadStaffsByUnitId(Long unitId,String humanType){
		return new Quokka(true, humanInfoService.loadStaffsByUnitId(unitId,humanType));
	}

	/**
	 * 人员加挂查询
	 * @param params
	 * @return
	 */
	@RequestMapping("searchAddStaff")
	public Quokka searchAddStaff(String params){
		return new Quokka(true,	humanInfoService.searchAddStaff(params));
	}

	/**
	 * 人员加挂
	 * @param huamnInfo
	 * @return
	 */
	@PostMapping("addStaff")
	public Quokka addStaff(@RequestBody HumanInfo huamnInfo){
		humanInfoService.addStaff(huamnInfo);
		return new Quokka(true);
	}

	/**
	 * 人员加挂撤销
	 * @param humanId
	 * @return
	 */
	@RequestMapping("delStaff")
	public Quokka delStaff(Long humanId){
		humanInfoService.delStaff(humanId);
		return new Quokka(true);
	}

	/**
	 * 重置密码
	 * @param humanId
	 * @return
	 */
	@RequestMapping("resetpassword")
	public Quokka resetPassword(Long humanId) {
		return new Quokka(true, humanInfoService.resetPassword(humanId));
	}
}
